package com.ll19.dao;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

/**
 * Base class for running DAO tests.
 * 
 * @author mraible
 */

// @ContextConfiguration 注解
// 查找Spring配置文件，并使用该配置文件启动 Spring 容器。
// @ContextConfiguration 注解有以下两个常用的属性：
// locations：可以通过该属性手工指定 Spring 配置文件所在的位置，可以指定一个或多个 Spring 配置文件。如下所示：
// @ContextConfiguration(locations={“xx/yy/beans1.xml”,” xx/yy/beans2.xml”})
// inheritLocations：是否要继承父测试用例类中的 Spring 配置文件，默认为 true。

// 创建对象会根据配置启动SPRING的IOC容器 并注入相关对象
// 同时实例化ResourceBundle

@ContextConfiguration(locations = {
		"classpath:/applicationContext-resources.xml",
		"classpath:/applicationContext-dao.xml",
		"classpath*:/applicationContext.xml",
		"classpath:**/applicationContext*.xml" })
// AbstractTransactionalJUnit4SpringContextTests
// Spring TestContext测试框架
public abstract class BaseDaoTestCase extends
		AbstractTransactionalJUnit4SpringContextTests {
	/**
	 * Log variable for all child classes. Uses LogFactory.getLog(getClass())
	 * from Commons Logging
	 */
	protected final Log log = LogFactory.getLog(getClass());
	/**
	 * ResourceBundle loaded from
	 * src/test/resources/${package.name}/ClassName.properties (if exists)
	 */
	// 可以使用java.util.ResourceBundle实现国际化
	// 这里用来加载UserManagerTest.properties
	protected ResourceBundle rb;

	/**
	 * Default constructor - populates "rb" variable if properties file exists
	 * for the class in src/test/resources.
	 */
	public BaseDaoTestCase() {
		// Since a ResourceBundle is not required for each class, just
		// do a simple check to see if one exists
		String className = this.getClass().getName();

		try {
			rb = ResourceBundle.getBundle(className);
		} catch (MissingResourceException mre) {
			// log.warn("No resource bundle found for: " + className);
		}
	}

	/**
	 * Utility method to populate a javabean-style object with values from a
	 * Properties file
	 * 
	 * @param obj
	 *            the model object to populate
	 * @return Object populated object
	 * @throws Exception
	 *             if BeanUtils fails to copy properly
	 */
	protected Object populate(Object obj) throws Exception {
		// loop through all the beans methods and set its properties from its
		// .properties file
		Map<String, String> map = new HashMap<String, String>();

		for (Enumeration<String> keys = rb.getKeys(); keys.hasMoreElements();) {
			String key = keys.nextElement();
			map.put(key, rb.getString(key));
		}

		BeanUtils.copyProperties(obj, map);

		return obj;
	}

	// 获取初始数据
	// protected Element getInitData(String tableName) throws Exception {
	// File inputXml = new File("classpath:/default-data.xml");
	// SAXReader saxReader = new SAXReader();
	//
	// Document document = saxReader.read(inputXml);
	// Element root = document.getRootElement();
	// Element initData = root.element(tableName);
	//
	// return initData;
	// }

}
